package org.spark.java.transformation;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.api.java.function.VoidFunction;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * 对集合数据每个单词拆分，每个单词单独一行
 *
 * @author: mdf
 * @Date: 2020/1/16 19:37
 * @Description:
 */
public class FlatMap {
    public static void main(String[] args) {
        SparkConf sparkConf = new SparkConf();
        //设置运行环境
        sparkConf.setMaster("local");
        sparkConf.setAppName("flatMap");
        JavaSparkContext context = new JavaSparkContext(sparkConf);
        //模拟数据
        List<String> list = Arrays.asList("saber", "hadoop", "spark");
        JavaRDD<String> listRdd = context.parallelize(list);
        JavaRDD<String> flatMap = listRdd.flatMap(new FlatMapFunction<String, String>() {
            public Iterator<String> call(String str) throws Exception {
                return Arrays.asList(str.split("\t")).iterator();
            }
        });
        flatMap.foreach(new VoidFunction<String>() {
            public void call(String str) throws Exception {
                System.out.println(str);
            }
        });
    }
}
